﻿@{
    @*Get userid*@
        var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
        Guid? user_id;
        if (uid != "")
        {
            user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
        }
        else
        {
            KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
            string un = User.Identity.Name;
            bcontrol.ressUserID(un);
            user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
        }
    @*Get userid*@    
    var listUser = ViewData["listUser"] as List<KimPhongCRM.Models.sp_Select_Combobox_User_Result>;
    var listTour = ViewData["listTour"] as List<KimPhongCRM.Models.sp_Select_Combobox_Tour_Result>;
    var listKhachHang = ViewData["listKhachHang"] as List<KimPhongCRM.Models.sp_Select_Combobox_KhachHang_Result>;

    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "ListTourKHView";
        settings.CallbackRouteValues = new { Controller = "TourKH", Action = "ListTourKHPartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "TourKH", Action = "ListTourKHPartialAddNew" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "TourKH", Action = "ListTourKHPartialUpdate" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "TourKH", Action = "ListTourKHPartialDelete" };
        
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.Width = 100;
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.CommandColumn.FixedStyle = GridViewColumnFixedStyle.Left;

        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "Thêm mới";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Thay đổi";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Xóa";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Lưu lại";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Hủy";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";

        settings.CommandColumn.Visible = true;
        settings.SettingsBehavior.ConfirmDelete = true;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;

        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.AllowResize = true;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Width = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Height = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Modal = true;
        settings.SettingsBehavior.ConfirmDelete = true;

        settings.KeyFieldName = "maTourKH";

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto;
        settings.Settings.VerticalScrollBarMode = ScrollBarMode.Hidden;


        settings.InitNewRow = (s, e) =>
        {
            e.NewValues["ngaySuaCuoi"] = System.DateTime.Now.Date;
            e.NewValues["ngayNhap"] = System.DateTime.Now.Date;
            e.NewValues["isActive"] = true;
            e.NewValues["nguoiNhap"] = user_id;
            e.NewValues["nguoiSuaCuoi"] = user_id;
        };
        //edit init
        settings.CellEditorInitialize = (sender, e) =>
        {
            //ASPxGridView gridView = sender as ASPxGridView;
            //if (sender == null || !gridView.IsNewRowEditing)
            //return;
            if (e.Column.FieldName == "nguoiSuaCuoi")
            {
                e.Editor.Value = user_id;
            }
            if (e.Column.FieldName == "ngaySuaCuoi")
            {
                e.Editor.Value = System.DateTime.Now.Date;
            }
        };
        settings.Columns.Add(column =>
        {
            column.FieldName = "maTourKH";
            column.Caption = "Mã chuyến đi";
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.Visible = false;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
         {
             column.FieldName = "maKH";
             column.Caption = "Khách hàng";
             column.ColumnType = MVCxGridViewColumnType.ComboBox;
             var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
             foreach (var value in listKhachHang)
             {
                 comboBoxProperties.Items.Add(value.tenKH, value.maKH);
             }
             comboBoxProperties.ValueType = typeof(long);

             column.SetDataItemTemplateContent(c =>
             {

                 object makh = DataBinder.Eval(c.DataItem, "maKH");
                 string url = Url.Action("SetPopup", "Popup", new { id = makh, table = "KhachHang" });
                 ViewContext.Writer.Write(
                    "<a href=\"" + url + "\" id=\"id-viewdetail\"  class=\"button-small btn-viewdetail\"><i class=\"font-icon-eye-open\"></i></a> " + c.Text
                );
             });
             column.Width = 100;
         });
        settings.Columns.Add(column =>
        {
            column.FieldName = "maTour";
            column.Caption = "Tour";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.ClientSideEvents.SelectedIndexChanged = "onComboBoxSelectedIndexChanged";
            foreach (var value in listTour)
            {
                comboBoxProperties.Items.Add(value.tenTour, value.maTour);
            }
            comboBoxProperties.ValueType = typeof(long);
            column.SetDataItemTemplateContent(c =>
            {
                object matour = DataBinder.Eval(c.DataItem, "maTour");
                string url = Url.Action("SetPopup", "Popup", new { id = matour, table = "Tour" });
                ViewContext.Writer.Write(
                   "<a href=\"" + url + "\" id=\"id-viewdetail\" class=\"button-small btn-viewdetail\"><i class=\"font-icon-eye-open\"></i></a> " + c.Text
               );
            });
            column.Width = 300;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "giaTien";
            column.Caption = "Giá Tour";
            column.ColumnType = MVCxGridViewColumnType.SpinEdit;
            var SpinEditPropertie = column.PropertiesEdit as SpinEditProperties;
            SpinEditPropertie.DisplayFormatString = "#";
            SpinEditPropertie.NumberType = SpinEditNumberType.Float;
            SpinEditPropertie.Increment = 1000;
            column.Visible = true;
            column.Width = 90;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngayDiTour";
            column.Caption = "Ngày đi";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            column.Visible = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
         {
             column.FieldName = "soDiem";
             column.Caption = "Điểm số";
             column.ColumnType = MVCxGridViewColumnType.SpinEdit;
             var SpinEditPropertie = column.PropertiesEdit as SpinEditProperties;
             SpinEditPropertie.DisplayFormatString = "#";
             SpinEditPropertie.NumberType = SpinEditNumberType.Float;
             SpinEditPropertie.Increment = 1000;
             column.Visible = true;
             column.Width = 90;
         });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngayNhap";
            column.Caption = "Ngày Nhập";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            column.Visible = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiNhap";
            column.Caption = "Người nhập";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.Visible = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngaySuaCuoi";
            column.Caption = "Ngày sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            column.Visible = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiSuaCuoi";
            column.Caption = "Người sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Visible = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "isActive";
            column.Caption = "Active";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Width = 60;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
                object matourKh_obj = DataBinder.Eval(c.DataItem, "maTourKH");
                string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = matourKh_obj, active = !actived, tablename = "tblTourKH", keyname = "maTourKH" });
                ViewContext.Writer.Write(
                    "<a href=\"" + updateLink + "\" id=\"id_button_active_" + matourKh_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
                );
            });
        });
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@grid.Bind(Model).GetHtml()